import { history } from 'umi';
import {
    fakeInformationCollection,
    fakeUpdateInformationCollection,
    fakeDeleteInformationCollection,
    fetchInformationCollectionDetail,
    fetchListBanner,
    fakeSaveBanner,
} from '@/services/MarketingModule';
import {
    fetchUserInformationCollection
} from '@/services/LiveStreamingServices';
import { notification } from 'antd';
const userInfo = {
    namespace: 'marketing',

    state: {

    },

    reducers: {
        save(state, { payload }) {
            return {
                ...state,
                ...payload,
            };
        },
        saveOpenMenu(state, { payload }) {
            const { menuItem } = payload;
            if (!state.openedMenu.find((i) => i.path === menuItem.path)) {
                const openedMenu = [...state.openedMenu];
                openedMenu.push(menuItem);
                return { ...state, openedMenu };
            }
            return state;
        },
    },
    effects: {
        *fetchListBanner({ payload, callback }, { call, put }) {
            const response = yield call(fetchListBanner, payload);
            if (!response) return;
            const { content = {} } = response;
            callback && callback(content);
        },
        *fakeSaveBanner({ payload, callback }, { call, put }) {
            const response = yield call(fakeSaveBanner, payload);
            if (!response) return;
            notification.success({
                message: '温馨提示',
                description: `保存成功`,
            });
            callback && callback();
        },

        *fetchUserInformationCollection({ payload, callback }, { call, put }) {
            const response = yield call(fetchUserInformationCollection, payload);
            if (!response) return;
            const { userInformationCollectionList, total } = response.content;
            yield put({
                type: 'save',
                payload: {
                    userInformationCollectionList: {
                        list: userInformationCollectionList,
                        total,
                    },
                },
            });
        },
        *fakeInformationCollection({ payload, callback }, { call, put }) {
            const { id } = payload;
            const response = yield call(
                id ? fakeUpdateInformationCollection : fakeInformationCollection,
                payload,
            );
            if (!response) return;
            notification.success({
                message: '温馨提示',
                description: id ? '修改成功' : `新增成功`,
            });
            callback && callback();
        },
        *fakeEnableInformation({ payload, callback }, { call, put }) {
            const response = yield call(fakeEnableInformation, payload);
            if (!response) return;
            notification.success({
                message: '温馨提示',
                description: `启用成功`,
            });
            callback && callback();
        },
        *fakeDisableInformation({ payload, callback }, { call, put }) {
            const response = yield call(fakeDisableInformation, payload);
            if (!response) return;
            notification.success({
                message: '温馨提示',
                description: `禁用成功`,
            });

            callback && callback();
        },
        *fakeDeletePublishMaterial({ payload, callback }, { call, put }) {
            const response = yield call(fakeDeletePublishMaterial, payload);
            if (!response) return;
            notification.success({
                message: '温馨提示',
                description: `删除成功`,
            });
            callback && callback();
        },
        *fakeDeleteInformationCollection({ payload, callback }, { call, put }) {
            const response = yield call(fakeDeleteInformationCollection, payload);
            if (!response) return;
            notification.success({
                message: '温馨提示',
                description: `删除成功`,
            });
            callback && callback();
        },

        *fetchInformationCollectionDetail({ payload, callback }, { call, put }) {
            const response = yield call(fetchInformationCollectionDetail, payload);
            if (!response) return;
            const { informationCollection } = response.content;
            callback && callback(informationCollection);
        },

    },
};
export default userInfo;
